package handlers

import (
	"car-contract-backend/cache"
	"car-contract-backend/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func RoleNodeAdd(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	if rid == 0 || nid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段为空",
		})
		return
	}
	data := models.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	models.DB.Create(&data).Debug()
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "角色权限关联成功",
	})
	return
}

/*
			SELECT
	  *

FROM

	roles
	LEFT JOIN user_roles ON user_roles.rid = roles.id
	WHERE user_roles.uid=1
*/
func ListUserRole(c *gin.Context) {
	uid, _ := strconv.Atoi(c.Query("uid"))
	var list []models.Role
	models.DB.Table("roles").Joins("LEFT JOIN user_roles ON user_roles.rid = roles.id").Where("user_roles.uid=?", uid).Debug().Find(&list)
	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
		"msg":  "查询成功",
	})
	return
}

/*
			SELECT
	  *

FROM

	nodes
	LEFT JOIN role_nodes ON role_nodes.nid = nodes.id
	WHERE role_nodes.rid=1
*/
func ListRoleNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.Query("rid"))
	var list []models.Node
	models.DB.Table("nodes").Joins("LEFT JOIN role_nodes ON role_nodes.nid = nodes.id").Where(" role_nodes.rid=?", rid).Debug().First(&list)
	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
		"msg":  "查询成功",
	})
	return
}

/*
		SELECT
	  warehouses.ware_name as WareName,
	  warehouses.ware_addr as WareAddr,
	  warehouses.ware_types as WareTypes,
	  orders.order_name as OrderName,
	  orders.order_num as OrderNum,
	  orders.wid as Wid

FROM

	orders
	LEFT JOIN warehouses ON warehouses.id=orders.wid
*/
func AllList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var all []models.WarehouseOrder
	redis_key := "list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").Select("warehouses.ware_name as WareName",
			"warehouses.ware_addr as WareAddr",
			"warehouses.ware_types as WareTypes",
			"orders.order_name as OrderName",
			"orders.order_num as OrderNum",
			"orders.wid as Wid").Joins("LEFT JOIN warehouses ON warehouses.id=orders.wid").Offset(offset).Limit(size).Debug().Find(&all)
		jsonData, _ := json.Marshal(all)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &all)
	}

	var total int64
	models.DB.Table("orders").Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"data":  all,
		"msg":   "列表成功",
		"total": total,
	})
	return
}
func AllDetail(c *gin.Context) {
	id := c.Query("id")
	var all models.WarehouseOrder
	redis_key := "detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").Select("warehouses.ware_name as WareName",
			"warehouses.ware_addr as WareAddr",
			"warehouses.ware_types as WareTypes",
			"orders.order_name as OrderName",
			"orders.order_num as OrderNum",
			"orders.wid as Wid").Joins("LEFT JOIN warehouses ON warehouses.id=orders.wid").Where("orders.id=?", id).Debug().Find(&all)
		jsonData, _ := json.Marshal(all)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &all)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"data": all,
		"msg":  "列表成功",
	})
	return
}
